package com.learn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.mapper.EmpMapper;
import com.learn.pojo.Emp;
import com.learn.pojo.PageBean;
import com.learn.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper EmpMapper;
    @Autowired
    private EmpMapper empMapper;
//    @Override
//    public PageBean selectByPage(Integer page, Integer pageSize) {
//        long total = EmpMapper.count();
//        int begin = (page - 1) * pageSize;
//        List list = EmpMapper.selectByPage(begin, pageSize);
//        return new PageBean(total, list);
//    }

    @Override
    public PageBean selectByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Emp> list = EmpMapper.list();
        Page<Emp> p = (Page<Emp>) list;
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public PageBean selectByPageAndCondition(Integer page, Integer pageSize, String name, Short gender, LocalDate begin,LocalDate end) {
        PageHelper.startPage(page, pageSize);
        List<Emp> list = EmpMapper.selectByCondition(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) list;
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
    }

    @Override
    public void add(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.add(emp);
    }

    @Override
    public Emp getDataById(Integer id) {
        return empMapper.getDataById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.selectByUsernameAndPassword(emp);
    }
}
